package controller

import (
	"sale-admin/config/mysql"
	"sale-admin/internal/app/web/service/activity"
	"sale-admin/internal/models"
	"time"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type Activity struct{}

// List 获取活动模板列表
func (a Activity) List(c *gin.Context) {
	var params struct {
		Page   int    `json:"page"`
		Limit  int    `json:"limit"`
		Title  string `json:"title"`
		ID     string `json:"id"`
		Status int    `json:"status"`
	}
	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	db := mysql.NewDB()
	data, count, err := activity.ActivityService{}.List(c.Request.Context(), db, merchantID, params.Title, params.ID, params.Status, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"data":  data,
		"count": count,
	}, err)
}

// Detail 获取活动详情
func (a Activity) Detail(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	db := mysql.NewDB()
	data, err := activity.ActivityService{}.Detail(c.Request.Context(), db, merchantID, params.ID)
	if err != nil {
		response.JSON(c, nil, err)
		return
	}

	now := time.Now().Unix()
	if now < data.PageStartTime {
		err = errorx.New("活动未开始", -1)
		response.JSON(c, nil, err)
		return
	}

	if now < data.PageStartTime {
		err = errorx.New("活动已结束", -1)
		response.JSON(c, nil, err)
		return
	}

	response.JSON(c, data, err)
}

// Detail 获取活动详情
func (a Activity) AdminDetail(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	db := mysql.NewDB()
	data, err := activity.ActivityService{}.Detail(c.Request.Context(), db, merchantID, params.ID)
	if err != nil {
		response.JSON(c, nil, err)
		return
	}

	response.JSON(c, data, err)
}

// Add 新建活动模板
func (a Activity) Add(c *gin.Context) {
	var params struct {
		ID            string `json:"id"`
		CouponBatchID string `json:"coupon_batch_id"`
		Title         string `json:"title"`
		ImagePath     string `json:"image_path"`
		Color         string `json:"color"`
		Content       string `json:"content"`
		Status        int    `json:"status"`

		//优惠券属性字段
		TemplateName   string                   `json:"template_name"`
		CouponBindType models.CouponBindTypeDef `json:"coupon_type"`
		GoodsID        int64                    `json:"goods_id,string"`
		GoodsClassID   int64                    `json:"goods_class_id,string"`
		OnlineEventID  int64                    `json:"online_event_id,string"`

		//优惠券总数、已用数量、库存数量
		TotalCouponCode int `json:"total_coupon_code"`

		CodeStartTime int64 `json:"code_start_time"`
		CodeEndTime   int64 `json:"code_end_time"`

		PageStartTime int64 `json:"page_start_time"`
		PageEndTime   int64 `json:"page_end_time"`

		CodeBackgroundImg string `json:"code_background_img"`
	}

	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	db := mysql.NewDB()
	err = activity.ActivityService{}.Save(
		c.Request.Context(),
		db,
		&models.MaActivity{
			ID:            params.ID,
			CouponBatchID: params.CouponBatchID,
			Title:         params.Title,
			ImagePath:     params.ImagePath,
			Color:         params.Color,
			Content:       params.Content,
			Status:        params.Status,

			MerchantID: merchantID,

			// 优惠券属性字段
			TemplateName:      params.TemplateName,
			CouponBindType:    params.CouponBindType,
			GoodsID:           params.GoodsID,
			GoodsClassID:      params.GoodsClassID,
			OnlineEventID:     params.OnlineEventID,
			TotalCouponCode:   params.TotalCouponCode,
			CodeStartTime:     params.CodeStartTime,
			CodeEndTime:       params.CodeEndTime,
			PageStartTime:     params.PageStartTime,
			PageEndTime:       params.PageEndTime,
			CodeBackgroundImg: params.CodeBackgroundImg,
		},
	)

	//merchantID,
	//		params.ID, params.CouponBatchID, params.Title, params.ImagePath, params.Color, params.Content, params.Status,

	response.JSON(c, nil, err)
}

// Edit 编辑活动模板
func (a Activity) Edit(c *gin.Context) {
	var params struct {
		ID            string `json:"id"`
		CouponBatchID string `json:"coupon_batch_id"`
		Title         string `json:"title"`
		ImagePath     string `json:"image_path"`
		Color         string `json:"color"`
		Content       string `json:"content"`
		Status        int    `json:"status"`

		//优惠券属性字段
		TemplateName   string                   `json:"template_name"`
		CouponBindType models.CouponBindTypeDef `json:"coupon_bind_type"`
		GoodsID        int64                    `json:"goods_id,string"`
		GoodsClassID   int64                    `json:"goods_class_id,string"`
		OnlineEventID  int64                    `json:"online_event_id,string"`

		//优惠券总数、已用数量、库存数量
		TotalCouponCode int `json:"total_coupon_code"`

		CodeStartTime int64 `json:"code_start_time"`
		CodeEndTime   int64 `json:"code_end_time"`

		PageStartTime int64 `json:"page_start_time"`
		PageEndTime   int64 `json:"page_end_time"`

		CodeBackgroundImg string `json:"code_background_img"`
	}

	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	db := mysql.NewDB()
	err = activity.ActivityService{}.Save(
		c.Request.Context(),
		db,
		&models.MaActivity{
			ID:            params.ID,
			CouponBatchID: params.CouponBatchID,
			Title:         params.Title,
			ImagePath:     params.ImagePath,
			Color:         params.Color,
			Content:       params.Content,
			Status:        params.Status,

			MerchantID: merchantID,

			// 优惠券属性字段
			TemplateName:      params.TemplateName,
			CouponBindType:    params.CouponBindType,
			GoodsID:           params.GoodsID,
			GoodsClassID:      params.GoodsClassID,
			OnlineEventID:     params.OnlineEventID,
			TotalCouponCode:   params.TotalCouponCode,
			CodeStartTime:     params.CodeStartTime,
			CodeEndTime:       params.CodeEndTime,
			PageStartTime:     params.PageStartTime,
			PageEndTime:       params.PageEndTime,
			CodeBackgroundImg: params.CodeBackgroundImg,
		})
	response.JSON(c, nil, err)
}

// Del 删除活动模板
func (a Activity) Del(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	db := mysql.NewDB()
	err = activity.ActivityService{}.Del(c.Request.Context(), db, merchantID, params.ID)
	response.JSON(c, nil, err)
}

// Status 修改活动状态
func (a Activity) Status(c *gin.Context) {
	var params struct {
		ID     string `json:"id"`
		Status int64  `json:"status"`
	}
	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	err = activity.ActivityService{}.Status(c.Request.Context(), merchantID, params.ID, params.Status)
	response.JSON(c, nil, err)
}

// AddData 新增客户资料
func (a Activity) AddData(c *gin.Context) {
	var params struct {
		Phone      string `json:"phone"`
		Code       string `json:"code"`
		ActivityID string `json:"activity_id"`
		Name       string `json:"name"`
		Title      string `json:"title"`
		Content    string `json:"content"`
	}
	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	redemptionCode, err := activity.ActivityService{}.AddData(
		c.Request.Context(),
		merchantID,
		params.Phone,
		params.Code,
		params.ActivityID,
		params.Name,
		params.Title,
		params.Content,
	)
	// 返回注册资料后的兑换码
	response.JSON(c, gin.H{"redemption_code": redemptionCode}, err)
}

// ListData 获取客户资料列表
func (a Activity) ListData(c *gin.Context) {
	var params struct {
		Page       int    `json:"page"`
		Limit      int    `json:"limit"`
		ActivityID string `json:"activity_id"`
		Phone      string `json:"phone"`
		StartTime  string `json:"start_time"`
		EndTime    string `json:"end_time"`
	}
	err := c.ShouldBindBodyWith(&params, binding.JSON)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	merchantID := c.GetString("merchant_id")
	data, count, err := activity.ActivityService{}.ListData(c.Request.Context(), merchantID, params.ActivityID, params.Phone, params.StartTime, params.EndTime, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"data":  data,
		"count": count,
	}, err)
}
